"""The AddPanel module contains the AddPanel class, which builds the GUI for the Add tab"""

import wx

class AddPanel(wx.Panel):
    def __init__(self, parent, id, plgen, settings):
        self.plgen = plgen
        self.settings = settings

        wx.Panel.__init__(self, parent, wx.ID_ANY)

        self.filetext = wx.StaticText(self, -1, "Enter the path to a file you want to add:", wx.Point(10,1))
        self.filebox = wx.TextCtrl(self, 20, "", wx.Point(10,20), wx.Size(340,-1))
        self.fileButton = wx.Button(self, -1, "Browse...", pos=(350, 20))
        self.Bind(wx.EVT_BUTTON, self.fileBrowse, self.fileButton)

        self.dirtext = wx.StaticText(self, -1, "Enter the path to a directory you want to add:", wx.Point(10,50))
        self.dirbox = wx.TextCtrl(self, 20, "", wx.Point(10,70), wx.Size(340,-1))
        self.dirButton = wx.Button(self, -1, "Browse...", pos=(350, 70))
        self.Bind(wx.EVT_BUTTON, self.dirBrowse, self.dirButton)

        self.alltagtext = wx.StaticText(self, -1, "Apply the following tag to all files added:", wx.Point(10,100))
        self.alltagbox = wx.TextCtrl(self, 20, "", wx.Point(10,120), wx.Size(140,-1))

        self.allratingtext = wx.StaticText(self, -1, "Apply the following tag to all files added:", wx.Point(10,150))
        self.allratingbox = wx.TextCtrl(self, 20, "3", wx.Point(10,170), wx.Size(140,-1))

        self.addButton = wx.Button(self, -1, "Add files", pos=(10, 210))
        self.Bind(wx.EVT_BUTTON, self.addFiles, self.addButton)

    def fileBrowse(self, event):
        dlg = wx.FileDialog(self, "Open video file...", self.plgen.getDirectory(), style=wx.OPEN, wildcard=self.settings["wildcards"]["all"])
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()
            self.filebox.SetValue(filename)
        dlg.Destroy()

    def dirBrowse(self, event):
        dlg = wx.DirDialog(None, "Choose a directory:", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        if dlg.ShowModal() == wx.ID_OK:
            dirname = dlg.GetPath()
            self.dirbox.SetValue(dirname)
        dlg.Destroy()
    
    def addFiles(self, event):
        tags = self.alltagbox.GetValue()
        rating = self.allratingbox.GetValue()
        
        filename = self.filebox.GetValue()
        self.filebox.SetValue("")
        
        dirname = self.dirbox.GetValue()
        self.dirbox.SetValue("")
        
        if filename == "":
            if dirname == "":
                dlg = wx.MessageDialog(None, "You have not selected a file nor a folder!", "Nothing to add!", style=wx.OK)
                dlg.ShowModal()
                dlg.Destroy()
            else:
                self.plgen.addDirectory(dirname, tags, rating)
        else:
            self.plgen.addFile(filename, tags, rating)

